﻿using System;
using System.IO;
using System.Linq;
using System.Web.UI.WebControls;
using System.Text;
using System.Collections;
using System.Web;
using System.Security.Cryptography;

public class CF //Common functions
{
    //Constructer
	public CF()
	{
	}

    //Encode functions
    /// <summary>
    /// 將字串編碼成Base64格式
    /// </summary>
    /// <param name="input">輸入字串</param>
    /// <returns>回傳Base64編碼後的字串</returns>
    public static string EncodeString(string strSource)
    {
        MemoryStream ms = new MemoryStream();
        DESCryptoServiceProvider key = new DESCryptoServiceProvider();
        CryptoStream encStream = new CryptoStream(ms, key.CreateEncryptor(Encoding.Default.GetBytes(strKey), Encoding.Default.GetBytes(strIV)), CryptoStreamMode.Write);
        StreamWriter sw = new StreamWriter(encStream);
        sw.WriteLine(strSource);
        sw.Close();
        encStream.Close();
        byte[] buffer = ms.ToArray();
        ms.Close();
        return Convert.ToBase64String(buffer);
    }
    /// <summary>
    /// 將字串由Base64格式編碼解碼成UTF-8 String類別
    /// </summary>
    /// <param name="input">輸入字串</param>
    /// <returns>回傳String類別的字串</returns>
    public static string DecodeString(string strSource)
    {
        MemoryStream ms = new MemoryStream(Convert.FromBase64String(strSource.Replace(" ", "+")));
        DESCryptoServiceProvider key = new DESCryptoServiceProvider();
        CryptoStream encStream = new CryptoStream(ms, key.CreateDecryptor(Encoding.Default.GetBytes(strKey), Encoding.Default.GetBytes(strIV)), CryptoStreamMode.Read);
        StreamReader sr = new StreamReader(encStream);
        string val = sr.ReadLine();
        sr.Close();
        encStream.Close();
        ms.Close();
        return val;
    }

    public static void WriteLoginLog(string sip_num)
    {
        string LogPath = HttpContext.Current.Server.MapPath(null)
            + CF.DoubleBackslash + CF.LoginLogDir + CF.DoubleBackslash;
        DirectoryInfo di = new DirectoryInfo(LogPath);
        if (!di.Exists)
            di.Create();

        string TodayFolder = DateTime.Now.ToString("yyyy-MM-dd");
        LogPath += TodayFolder + CF.DoubleBackslash;
        di = new DirectoryInfo(LogPath);
        if (!di.Exists)
            di.Create();

        string TodayFile = DateTime.Now.ToString("HH-mm-ss") + "-" + sip_num + "登入.txt";
        using (StreamWriter OutFile = new StreamWriter(LogPath + TodayFile))
        {
            OutFile.Write(sip_num + "已登入。");
        }
    }
    //靜態參數
    //路徑相關參數
    public static string SurveyPage = "SurveyWeb.aspx";
    public static string RedirectPage = "SurveyLogin.aspx";
    public static string DoubleBackslash = "\\";

    //iSender登入相關參數
    public static string PostURL = "http://ios.ccp.com.tw/Chris/ccpsip/Message/robot_send_text.ashx";
    public static string ServiceID = "LoginSurvey";
    public static string ServicePassword = "XpkCxxdSkfuBF7caVUjZ";
    public static string IsenderLoginSuccessedMessage = "您已登入Web問卷調查。";
    public static string LoginLogDir = "login_log";

    //QueryString、加密相關參數
    public static string QS_sip_number = "lo8t5dq";
    public static string QS_time_stamp = "oiw06grfwr";
    public static string QS_custom_name = "ag6ivwdw4f";
    public static string QS_user_dir = "f8u9k5klqj";
    public static string QS_survey_id = "cieudsj6d";
    
    /*
    public static string QS_sip_number = "SipNumber";
    public static string QS_time_stamp = "TimeStamp";
    public static string QS_custom_name = "CustomName";
    public static string QS_user_dir = "UserDir";
    public static string QS_survey_id = "SurveyId";
    */

    private static string strKey = "kizuki33";
    private static string strIV = "takamiya";
}